package com.hoyue.dao.impl;

import com.hoyue.bean.User;
import com.hoyue.dao.UserDao;
import com.hoyue.utils.C3P0Utils;
import com.hoyue.utils.MD5;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;


import java.sql.SQLException;

/**
 * @author Hoyue_network1
 * @version 1.0
 * Creat by 2018/8/23 17:35
 */
public class UserDaoImpl implements UserDao{
	QueryRunner qr = C3P0Utils.getQueryRunner();

	public User loginUser(String userName, String password) throws SQLException {

		String sql = " SELECT * FROM user WHERE username = ? AND password = ? ";

		return qr.query(sql,new BeanHandler<>(User.class),userName, MD5.EncoderByMd5(password));
	}

	@Override
	public void update(User user) throws SQLException {
		String sql = "UPDATE user SET username=?, picPath=? WHERE id=?";

		qr.update(sql, user.getUserName(), user.getPicPath(),user.getId());
	}

	public void xiuGaiPsw(int id, String psw) throws SQLException {
		String sql = "UPDATE user SET password=? WHERE id=?";

		qr.update(sql,MD5.EncoderByMd5(psw),id);
	}

}
